
import java.util.ArrayList;
import java.util.List;
// https://leetcode.cn/problems/binary-tree-paths/
public class TreePaths {
    private static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val)
        {
            this.val = val;
        }
    }
    class Solution {
        public List<String> binaryTreePaths(TreeNode root) {

            List<String> res = new ArrayList<>();
            if(root == null) return res;
            List<Integer> path = new ArrayList<>();
            path.add(root.val);
            traversal(root,path,res);
            return res;
        }
        private void traversal(TreeNode node, List<Integer> list, List<String> res)
        {
            if(node.left == null && node.right == null)
            {
                StringBuilder sb = new StringBuilder();
                for(int v: list)
                {
                    sb.append(v);
                    sb.append("->");
                }
                res.add(sb.substring(0,sb.length()-2).toString());

            }
            if(node.left != null)
            {
                list.add(node.left.val);
                traversal(node.left,list,res);
                list.remove(list.size()-1);
            }
            if(node.right != null)
            {
                list.add(node.right.val);
                traversal(node.right,list,res);
                list.remove(list.size()-1);
            }

        }

    }
}
